<template>
  <div>
    <div>
      
      
      <el-table :data="tableData" border style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column prop="name" label="序号" width="60">
          <template slot-scope="scope">{{scope.$index+1}}</template>
        </el-table-column>
        <el-table-column prop="name" label="发布时间" width="100">
          <template slot-scope="scope">{{timeFun(scope.row.created_at)}}</template>
        </el-table-column>
        <el-table-column prop="img_url" label="封面" width="90">
          <template slot-scope="scope">
            <img :src="scope.row.img_url" style="width:70px;height:80px"/>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="内容"> </el-table-column>      
        <el-table-column prop="page_view" label="点击量" width="70">> </el-table-column>      
        <el-table-column prop="status" label="状态" width="50">
          <template slot-scope="scope">
            
            <span style="color:#67c23a" v-if="scope.row.status==1">上线</span>
            <span v-if="scope.row.status==2">下线</span>
            <span style="color:#fd0767" v-if="scope.row.isRecommend == 1">推荐</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope" >
            <div>
              <!-- <el-button class="addBtn" @click="tickFun(scope.row)"> 置顶</el-button> -->
              <el-button class="addBtn" @click="editFun(scope.row)" v-if="userRole.BN_101002"> 编辑</el-button>
              <el-button  v-if="scope.row.status==1" type="danger"  class="addBtn" @click="deletData(scope.row.id,2)"> 下线</el-button>
              <el-button  v-if="scope.row.status==0" type="danger"  class="addBtn" @click="deletData(scope.row.id,1)"> 上线</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
     
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  import {mapActions} from 'vuex'
  import Http from '@/Http/Http'
  import getTime from '@/Http/getTime'

  export default {
    data() {
      return {
        userRole:JSON.parse(sessionStorage.getItem('userSolt')),
        articelStatus:false,
        dataBanner:'',
        currentPage4: 1,//分野  
        sheachValue:'',
        total:0,      
        tuijians:'',
        seachtype:0,
        value:'全部',
        
        tableData: [],
        page:1,
        pageSize:10
      }
    },
    props:['navStatus'],

    created() {
      this.getlist()
    },
    methods: {
      getlist() {
        const parm ={
          "pageIndex":this.page,
          "pageSize":this.pageSize,
          "keyword":this.sheachValue,
        };
        if(this.navStatus == 0){
          parm.status = 1;
        } else if(this.navStatus == 1) {
           parm.status = 2;
        } else {
          parm.status ='';
        }
        Http.getBannerlList(parm)
        .then( res => {
          this.tableData = res.data.data.rows;
          this.total = res.data.data.total;
           this.dataBanner = '';
          this.articelStatus = false
          
        })
        .catch( error => {
          
        })
      },  
      
      timeFun(val) {
        return getTime(val)
      },
     
      tickFun(row) {
        Http.postStickAritice(row.articleId)
        .then( res => {
          if( res.data.code == 200 ){
            this.$message.success('置顶成功')
            this.getlist()
          } else if(res.data.code == -1003) {
            this.$message.error(`请求失败原因：${res.data.message}`)
             setTimeout(()=>{
              this.$router.push('/')
            },2000)
          }
       })
       .catch( res => {
         this.$message.error('服务器请求失败')
       })
      },
      editFun(val) {
        this.$emit('goEdit',val)
      },
      deletData(row,val) {
        let txt='';
        if(val==1){
          txt = '上线'
        } else {
          txt = '下线'
        };
        this.$confirm(`确认${txt}此banner吗 ？`)
        .then(_ => {
          Http.editBanner({
            "id":row,
            "status":val
          })
          .then( res => {
            
            if( res.data.code == 200 ){
              this.$message.success(`${txt}成功`)
              this.getlist()         
            } else if(res.data.code == -1003) {
              this.$message.error(`请求失败原因：${res.data.message}`)
              this.$router.push('/')
            }
          })
          .catch( error => {
            this.$message.error('服务器请求失败')
          })
        })
        .catch(_ => {
          //取消部分
          
        });
      },
      //选择 推荐
      handleSelectionChange(val) {
        this.tuijians = val;
      },
      editTui() {

      },
      chageStatus(val) {
        let txt ='';
        val? txt ='开启' : txt = '关闭';
        this.$confirm(`确认 ${txt} 此文章吗 ？`)
        .then(_ => {
        })
        .catch(_ => {
          
          
        });
      
        
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
       //搜索
      sheachFun(val){
        this.sheachValue = val;
        this.getlist()
      },
      //排序
      sortBtn() {
        this.sortStatus = true
      },
      sorlCheckStatus(){
        this.sortStatus = false
      },
       //分页
      handleSizeChange(val) {
        this.pageSize = val;
        this.getlist()
        
      },
      handleCurrentChange(val) {
        this.page = val;
        this.getlist()
        
      },
      //搜索
      seachFun() {
        
        this.getlist()
      }
    }
  }
</script>